<?php
namespace Admin\Controller;
use Think\Controller;
class ClubController extends Controller {
    public function index(){
        echo 'Hello Admin';
    }
    public function clublist(){
        //调用模型层获取数据
        $usermodel = M('club');//M操作模型层的方法,()内为数据库表的名字
        //var_dump($usermodel);
        $user = $usermodel->order('clubid desc')->select();
        //调用视图层显示数据
        $this->user=$user;
        //var_dump($user);
        $this->display();
    }
    public function delclub($id){
        //调用模型层获取数据
        $usermodel = M('club');
        $userinfo=$usermodel->where("clubid=$id")->find();
        if ($userinfo["clubphoto"]=="default.jpg") {

        }else{
            unlink(realpath("./Public/Uploads/clubphoto/".$userinfo["clubphoto"]));
        }
        if (false !== $usermodel->where("clubid=$id")->delete()) {
            $this->success('删除成功！');//success显示笑脸
        } else {
            $this->error('删除失败 ');//显示一个哭脸
        }
    }


    public function myupload(){
        $upload=new \Think\Upload();//实例化ThinkPhp自带上传类
        $upload->maxSize = 3145728;//设置上传附件大小
        $upload->exts = array('jpg','jpeg','png','gif');//设置附件上传类型
        $upload->rootPath = './Public/Uploads/clubphoto/';
        //$upload->savePath='';
        $upload->autoSub =false;
        
        $upload->saveName = time().'_'.mt_rand();
        $info = $upload->upload();  // 上传文件
        var_dump($info);
        if (!$info) {
            $this->error($upload->getError());
        }
        else{
            return $info;
        }
    }

    public function doclubadd(){
        $clubadd=M('club');
        if ($clubadd->create()) {
            if ($_FILES['file']['error']==0) {
                $logoinfo = $this->myupload();
                $pname = $logoinfo['file']['savename'];
                /*dump($logoinfo);
                echo "$pname";
                exit;*/
            }
            else{
                $pname="default.jpg";
            } 
            $image = new \Think\Image();
            $image->open('./Public/Uploads/clubphoto/'.$pname);
            $image->thumb(150,150)->save('./Public/Uploads/clubphoto/'.$pname);
            $clubadd->clubphoto=$pname;
            //var_dump($clubadd);
            if (false!==$clubadd->add()){
                //echo $clubadd->getLastSql();
                $this->success("添加成功",U("Club/clublist"));
            }
            else
            {
                $this->error('添加错误'); 
            }
        }
        else{
            $this->error($clubadd->getError());
        }
    }


    public function editclub($id)
    {       
        $clubmodel = M('club');
        $res=$clubmodel->where("clubid=$id")->find();
        if ($res["clubphoto"]=="default.jpg") {

        }else{
            unlink(realpath("./Public/Uploads/clubphoto/".$res["clubphoto"]));
        }
        $this->clubinfos=$res;
        $this->display();
   }

    public function doUpdate($id){
        /*$id=$_GET('clubid');
        var_dump($id);*/
        $club=M("club");
        $res = $club->where("clubid=$id")->find();
        //var_dump($id);

        if ($club->create()) {
        $data['clubname' ]=I('post.clubname');
        $data['clubplace' ]=I('post.clubplace');
        $data['clubnote' ]=I('post.clubnote');
        $data['clubphone' ]=I('post.clubphone');
        $data['clubtime' ]=I('post.clubtime');
        $reg=$club->where("clubid=$id")->save($data);
        /*echo $users->getLastSql();*/
        if (false!==$reg) {
            $this->success('修改成功','../Club/clublist');
        }
        else
        {
           $this->error('修改错误','../Club/clublist'); 
        }  
}

}

    /*public function editpswd($id){
        $usermodel=M('user');//创建一个对象或交实例化：将表的结构存到内存上 
        $usermodel->password=md5("960206");
        if(false!==$usermodel->where("id=$id")->save()){
            //false不恒等用于精确和0进行比较
            //save保存，将值保存到数据表中。
            $this->success('密码重置成功！');
        }else{
            $this->error('密码重置失败');
        }
    }
    public function changestate($id,$state){
        $usermodel=M('user');//创建一个对象或交实例化：将表的结构存到内存上 
        $usermodel->state=($state+1)%2;
        if(false!==$usermodel->where("id=$id")->save()){
            //false不恒等用于精确和0进行比较
            //save保存，将值保存到数据表中。
            $this->success('状态修改成功！');
        }else{
            $this->error('状态修改失败');
        }
    }*/


    public function search(){
        $clubmodel=M('club');
        $clubname = $_POST['clubname'];
        
        if($clubname)
        {
            $where['clubname']=array('like',"%$clubname%");
            $club = $clubmodel->where($where)->select();
        }
        else
        {$club=$clubmodel->order('clubid desc')->select();}
        $this->club=$club;
        $this->display();
    }
    
}





